package com.winspread.app.appbarlayoutui

import com.google.android.material.appbar.AppBarLayout
import kotlin.math.abs

/**
 *Author: Amin
 *Data: 2021/9/27 18:17
 *
 *Description:
 *
 */
open class AppBarStateChangeListener:AppBarLayout.OnOffsetChangedListener {

    enum class State {
        EXPANDED, COLLAPSED, IDLE
    }

    private var mCurrentState = State.IDLE

    override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
        when {
            verticalOffset == 0 -> {
                if (mCurrentState != State.EXPANDED) {
                    onStateChanged(appBarLayout, State.EXPANDED,verticalOffset);
                }
                mCurrentState = State.EXPANDED;
            }
            abs(verticalOffset) >= appBarLayout!!.totalScrollRange -> {
                if (mCurrentState != State.COLLAPSED) {
                    onStateChanged(appBarLayout, State.COLLAPSED,verticalOffset);
                }
                mCurrentState = State.COLLAPSED;
            }
            else -> {
                if (mCurrentState != State.IDLE) {
                    onStateChanged(appBarLayout, State.IDLE,verticalOffset);
                }
                mCurrentState = State.IDLE;
            }
        }

    }
    open fun onStateChanged(appBarLayout: AppBarLayout?, state: State?, verticalOffset: Int) {}
}